#!/usr/bin/env python
# coding: utf-8

import xml.dom.minidom
from xml.dom.minidom import Node
import re
import os

CFGFILE_TYPE_XML = "XML"
CFGFILE_TYPE_TXT = "TXT"

class tnxxx_ne_info_parse(object):
    def __init__(self,cfgfile):        
        if [] != re.findall(".txt", cfgfile):
            self.cfgfile_type = CFGFILE_TYPE_TXT
        else:
            self.doc = xml.dom.minidom.parse(cfgfile)
            self.cfgfile_type = CFGFILE_TYPE_XML
        self.cfgfile_name = cfgfile
    
    
    def get_nes_info(self):
        if self.cfgfile_type == CFGFILE_TYPE_TXT:
            return self.get_nes_info_from_iplist()
        
        self.nes_info = {}          
       
        nes_nodes=self.doc.getElementsByTagName("nes-info")[0]
        
        for ne_node in nes_nodes.getElementsByTagName("ne"):
            ne_info = {}
            ne_info["id"]       = ne_node.getAttribute("id")
            ne_info["ip"]       = ne_node.getAttribute("shelfip")
            ne_info["type"]     = ne_node.getAttribute('type')
            
            #增加单板信息
            chss_dict = {}
            chss_info = ne_node.getElementsByTagName("chss-info")[0]
            for chss in chss_info.getElementsByTagName("chss"):
                chss_id = chss.getAttribute("id")
                chss_dict[chss_id]          = {}
                chss_dict[chss_id]["id"]    = chss_id    
                chss_dict[chss_id]["type"]  = chss.getAttribute("type")
                chss_dict[chss_id]["slot"]  = chss.getAttribute("slot")
            
            ne_info["chss"]     = chss_dict
            self.nes_info[ne_info["id"]] = ne_info
            
        return self.nes_info
    
    def get_iplist(self):
        iplist_filted = []                
        if os.path.isfile(os.getcwd()+r'/'+ self.cfgfile_name):
            iplist = open(os.getcwd()+r'/'+ self.cfgfile_name)           
        else:     
            return iplist_filted 
        
        for line in iplist:
            if re.match(r'^\s*$', line) is not None : continue # 忽略空白行
            if re.match(r'^\s*#', line) is not None : continue # 忽略注释行            
            iplist_filted.append(line.strip('\n'))        
            
        iplist.close()
        
        return iplist_filted  
    
    #从ip地址列表定制705产品的nes_info    
    def get_nes_info_from_iplist(self):
        self.nes_info = {}
        
        iplist = self.get_iplist()
        
        for i,ip in enumerate(iplist):
            ne_info = {}
            ne_info["id"]       = unicode(i+1)
            ne_info["ip"]       = unicode(ip)
            ne_info["type"]     = unicode("TN705")
            
            #增加单板信息
            chss_dict = {}            
            for chss in [3,4]:
                chss_id = unicode(chss)
                chss_dict[chss_id]          = {}
                chss_dict[chss_id]["id"]    = chss_id    
                chss_dict[chss_id]["type"]  = unicode("OPCA")
                chss_dict[chss_id]["slot"]  = chss_id
            
            ne_info["chss"]     = chss_dict
            self.nes_info[ne_info["id"]] = ne_info
            
        return self.nes_info
    
if __name__ == "__main__" :
    xml=tnxxx_ne_info_parse("D:\\HeavySmoker\\workspace\\suites\\suite_tn7x5\\tn7x5.xml")
    nes = xml.get_nes_info()
   
    